package com.xin.libhtmlcontent.web;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebView;

import com.xin.libhtmlcontent.tv.OnImgCallBack;
import com.xin.libhtmlcontent.web.lib.WebViewUtils;

import java.util.Arrays;
import java.util.List;

public class WebViewHtmlContent extends WebView {
    private WebViewUtils webViewUtils;
    private boolean hasLoadFinish;
    private String htmlContent;
    private OnImgCallBack onImgCallBack;

    public WebViewHtmlContent(Context context) {
        this(context, null);
    }

    public WebViewHtmlContent(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WebViewHtmlContent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void setHtmlContent(String html) {
        if (hasLoadFinish) {
            initH5Body(html);
        }
        htmlContent = html;
    }

    /**
     * 加载消息
     *
     * @param infoContent
     */
    protected void initH5Body(String infoContent) {
        infoContent = infoContent.replaceAll("\n", "");
        String jsBody = "$(\"#content\").replaceWith('" + infoContent + "')";
        String jsCommand = "javascript:" + jsBody;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            evaluateJavascript(jsCommand, new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    onLoadBodySuccess();
                }
            });
        } else {
            loadUrl(jsCommand);
            onLoadBodySuccess();
        }
    }

    public WebViewHtmlContent setOnImgCallBack(OnImgCallBack onImgCallBack) {
        this.onImgCallBack = onImgCallBack;
        return this;
    }

    private void onLoadBodySuccess() {
        postDelayed(new Runnable() {
            @Override
            public void run() {
                webViewUtils.loadJs("onLoadBodySuccess()");
            }
        }, 1000);
    }

    private void initView() {
        Activity activity = (Activity) getContext();
        webViewUtils = new WebViewUtils(activity) {
            @Override
            protected void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (url.endsWith("ht5.html")) {
                    hasLoadFinish = true;
                    if (htmlContent != null) {
                        setHtmlContent(htmlContent);
                    }
                }
            }
        };
        webViewUtils.initWebView(this);
        webViewUtils.initWebClient();
        loadUrl("file:///android_asset/html_view/ht5.html");
        AndroidCallBack object = new AndroidCallBack();
        addJavascriptInterface(object, "Android");
    }

    private void log(String s) {
        System.out.println(s);
    }

    public class AndroidCallBack {


        @JavascriptInterface
        public void onImgClick(String currentSrc, String[] scrList) {
            log("onImgClick() called with: currentSrc = [" + currentSrc + "], scrList = [" + scrList + "]");
            if (onImgCallBack != null) {
                List<String> ls = Arrays.asList(scrList);
                onImgCallBack.onImgClick(ls, ls.indexOf(currentSrc));

            }
        }

        @JavascriptInterface
        public void test() {
            log("test() called");
        }


    }
}
